package cn.yeziji.forum.utils;

import cn.yeziji.forum.common.ForumResult;

import java.lang.reflect.Field;
import java.util.*;

/**
 * 条件构造工具类
 *
 * @author gzkemays
 * @since 2022/1/13 21:10
 */
public class IQueryUtils {
  private static final String DEFAULT_EXS_COLUMN = "serialVersionUID";

  public static String[] getExColumns(Class<?> clazz, String... exs) {
    Set<String> exsList = new HashSet<>(Arrays.asList(exs));
    Field[] declaredFields = clazz.getDeclaredFields();
    List<String> list = new ArrayList<>();
    for (Field field : declaredFields) {
      // 获取字段
      field.setAccessible(true);
      String fieldName = field.getName();
      if (!exsList.contains(fieldName) && !fieldName.equals(DEFAULT_EXS_COLUMN)) {
        list.add(fieldName);
      }
    }
    if (!list.isEmpty()) {
      return list.toArray(new String[0]);
    }
    throw new NullPointerException("指定获取字段失败");
  }

  public static void main(String[] args) {
    System.out.println(Arrays.toString(getExColumns(ForumResult.class, "code")));
  }
}
